package com.data.modules.smzh.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.data.modules.smzh.entity.Buildinguser;
import com.data.modules.smzh.entity.SmUser;
import com.data.modules.smzh.entity.UserCard;
import com.data.modules.smzh.entity.UserFace;
import com.data.modules.smzh.mapper.SmUserMapper;
import com.data.modules.smzh.service.IBuildinguserService;
import com.data.modules.smzh.service.ISmUserService;
import com.data.modules.smzh.service.IUserCardService;
import com.data.modules.smzh.service.IUserFaceService;
import com.data.modules.smzh.vo.SmUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * @Description: user
 * @Author: data-boot
 * @Date:   2022-11-27
 * @Version: V1.0
 */
@Service
public class SmUserServiceImpl extends ServiceImpl<SmUserMapper, SmUser> implements ISmUserService {

    @Autowired
    private IUserFaceService userFaceService;
    @Autowired
    private IUserCardService cardService;

    @Autowired
    private IBuildinguserService buildUserService;
    @Autowired
    private SmUserMapper smUserMapper;
    @Override
    public  IPage<SmUserVo> pageList(Page<SmUserVo> page, SmUserVo user){


        return  smUserMapper.queryPageList(page,user);
    }

    /**
     * 新增用户基本信息
     * @param user
     */
    @Override
    @Transactional
    public void addUserInfo(SmUserVo user) {
        SmUser smUser = new SmUser();

        BeanUtil.copyProperties(smUser,user);

        smUserMapper.insert(smUser);
        if(ObjectUtil.isNotEmpty(smUser.getId())){
            //新增人脸信息
            if(ObjectUtil.isNotEmpty(user.getFaseImage())){

                UserFace userFace = new UserFace();
                userFace.setUserId(smUser.getId());
                userFace.setImagePath(user.getFaseImage());
                userFaceService.save(userFace);

            }

            if(ObjectUtil.isNotEmpty(user.getCardNo())){

                UserCard userCard = new UserCard();
                userCard.setUserId(smUser.getId());
                userCard.setCardType(1);
                userCard.setCardNumber(user.getCardNo());
                cardService.save(userCard);
            }

            if(ObjectUtil.isNotEmpty(user.getUniqueNo())){

                Buildinguser buildinguser = new Buildinguser();
                buildinguser.setUserId(smUser.getId());
                buildinguser.setUniqueNo(user.getUniqueNo());
                buildUserService.saveOrUpdate(buildinguser);
            }

        }

    }

    @Override
    public IPage<SmUserVo> queryBindUserPageList(Page<SmUserVo> page, SmUserVo user) {

        return  smUserMapper.getBindUserPageList(page,user);
    }

    /**
     * 通过设备编号查询 绑定人员信息
     * @param page
     * @param user
     * @return
     */
    @Override
    public IPage<SmUserVo> queryBindUserBySerNoPageList(Page<SmUserVo> page, SmUserVo user) {

        return  smUserMapper.getBindUserBySerNoPageList(page,user);
    }
}
